package com.cskaoyan.mall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.product.dto.SkuInfoPageDTO;
import com.cskaoyan.mall.product.dto.SpuImageDTO;
import com.cskaoyan.mall.product.dto.SpuSaleAttributeInfoDTO;
import com.cskaoyan.mall.product.model.SkuInfo;
import com.cskaoyan.mall.product.query.SkuInfoParam;
import com.cskaoyan.mall.product.service.SkuService;
import com.cskaoyan.mall.product.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/7/1 21:01
 */
@RestController
public class AdminSkuController {

    @Autowired
    SpuService spuService;
    @Autowired
    SkuService skuService;

    //1.商品图片的回显
    @GetMapping("admin/product/spuImageList/{spuId}")
    public Result<List<SpuImageDTO>> getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImageDTO> spuImageList = spuService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //2. 根据SpuId查询SPU与销售属性关系表中包含的销售属性以及销售属性值集合
    @GetMapping("admin/product/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttributeInfoDTO>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {

        List<SpuSaleAttributeInfoDTO> spuSaleAttrList = spuService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //3. SKU数据的保存
    @PostMapping("admin/product/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoParam skuInfo) {
        // 调用服务层
        skuService .saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //4. SKU基本信息列表查询
    @GetMapping("admin/product/list/{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit) {

        //4.1 构建分页参数对象
        Page<SkuInfo> pageParam = new Page<>(page, limit);
        //4.2 根据分页参数查询SKU分页数据
        SkuInfoPageDTO skuInfoPageDTO = skuService.getPage(pageParam);

        return Result.ok(skuInfoPageDTO);
    }

    //5. SKU商品的上架
    @GetMapping("admin/product/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {

        skuService.onSale(skuId);
        return Result.ok();
    }

    //6. SKU商品的下架
    @GetMapping("admin/product/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {

        skuService.offSale(skuId);
        return Result.ok();
    }
}
